from selenium import webdriver
import time
import sys
import smtplib
from email.mime.text import MIMEText
from email.header import Header

def header():
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument('--no-sandbox')
    browser = webdriver.Chrome()
    browser.implicitly_wait(8)
    return browser
def mail(to_addr):
    try:
        from_addr = 'mail'
        password = 'secure_code'
        smtp_server = 'smtp.gmail.com'
        msg = MIMEText('今日健康打卡已完成[by:打卡助手]', 'plain', 'utf-8')
        msg['From'] = Header(from_addr)
        msg['To'] = Header(to_addr)
        msg['Subject'] = Header('COV疫情打卡助手')
        server = smtplib.SMTP_SSL()
        server.connect(smtp_server, 465)
        server.login(from_addr, password)
        server.sendmail(from_addr, to_addr, msg.as_string())
        server.quit()
        print("sent")
    except Exception as e:
        print(e)

if __name__ == '__main__':
    toaddr = "to_mail"
    i = 0
    browser = header()
    browser.get('https://jksb.v.zzu.edu.cn/vls6sss/zzujksb.dll/first0')
    while browser.page_source.find('111111') == -1:
        time.sleep(2)
        if browser.page_source.find('proceed-link') != -1:
            browser.find_element_by_css_selector("#proceed-link").click()
        time.sleep(2)
        i = i + 1
        print(i)
        browser.close()
        browser = header()
        browser.get('https://jksb.v.zzu.edu.cn/vls6sss/zzujksb.dll/first0')
    print(f"browser text = {browser.page_source}")
    browser.find_element_by_xpath("//input[@name='uid']").send_keys("sid")
    browser.find_element_by_xpath("//input[@name='upw']").send_keys("spass")
    browser.find_element_by_xpath("//input[@type='submit']").click()
    time.sleep(1)
    browser.switch_to.frame(browser.find_element_by_xpath("//iframe[@id='zzj_top_6s']"))
    time.sleep(1)
    browser.find_element_by_xpath("//form[@name='myform52']").submit()
    time.sleep(1)
    browser.find_element_by_xpath("//select[@name='myvsp_5']").send_keys("正常")
    time.sleep(1)
    browser.find_element_by_xpath("//form[@name='myform52']").submit()
    if browser.page_source.find("今日您已经填报过了") != -1:
        mail(toaddr)
        sys.exit()
